package cn.lyh.iotmanage.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.lyh.iotmanage.dao.EmqSessionsubDao;
import cn.lyh.iotmanage.dao.EmqSessionsubDaoMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lyh
 * @version 1.0
 * @since 2021/5/16
 */
@Service
public class EmqSessionSubService extends ServiceImpl<EmqSessionsubDaoMapper,EmqSessionsubDao> {
    /**
     * 此方法用于存储处理会话订阅主题消息，将转发过来的数据处理并存入mysql
     * @param params 前端传入的数据体
     */
    public void saveSubscribedInfo(Map<String, Object> params) {
        // 获取时间轴并转换
        Object timestamp = params.get("timestamp");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(timestamp);
        DateTime timeParse = DateUtil.parse(format);
        // map拷贝到Bean上
        Map<String,String> mapping = new HashMap<>();
        mapping.put("topic","topicName");
        EmqSessionsubDao emqSessionsubDao = BeanUtil.toBean(params, EmqSessionsubDao.class, CopyOptions.create().setFieldMapping(mapping));
        // 设置格式化后的时间
        emqSessionsubDao.setSubTime(timeParse);
        System.out.println("emqSessionsubDao = " + emqSessionsubDao);
        // 存入mysql
        this.saveOrUpdate(emqSessionsubDao);

    }
}
